package cn.w.outdoor.demo;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import cn.w.outdoor.lib.IntercomSystem;
import cn.w.outdoor.lib.common.CameraTools;
import cn.w.outdoor.lib.common.SingleThreadPoll;
import cn.w.outdoor.lib.common.TimeCountDownTools;
import cn.w.outdoor.lib.common.TimeCountDownUnit;
import cn.w.outdoor.lib.media.H264VideoCollector;
import cn.w.outdoor.lib.media.H264VideoPlayer;
import cn.w.outdoor.lib.media.OnDataListener;
import cn.w.outdoor.lib.model.Trans_Package;
import cn.w.outdoor.lib.network.NetDefine;
import cn.w.outdoor.lib.network.TCPTools;
import cn.w.outdoor.lib.network.UDPTools;

public class MainActivity extends Activity {

    private final static String TAG = "MainActivity";

    private H264VideoPlayer h264VideoPlayer = new H264VideoPlayer();
    private H264VideoPlayer h264VideoPlayer2 = new H264VideoPlayer();
    private H264VideoPlayer h264VideoPlayer3 = new H264VideoPlayer();
    private H264VideoPlayer h264VideoPlayer4 = new H264VideoPlayer();
    private H264VideoCollector h264VideoCollector = new H264VideoCollector();

    private RingingTimeCountDownUnit ringingTimeCountDownUnit = null;

    private class RingingTimeCountDownUnit extends TimeCountDownUnit {

        public RingingTimeCountDownUnit() {
            setWaitTimeLimit(45); // 此部分注意，远端为 55 秒退出，此部分事件如果小于 55 秒，需要发送挂断指令到远端设备
        }

        @Override
        public void action(int timeLimit) {
            Log.e(TAG, "响铃剩余事件: " + timeLimit);
            if (timeLimit == 0) {
                stopVideo();
                IntercomSystem.getInstance().hangup(NetDefine.REMOT_IP_ADDR);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //region UI部分初始化
        SingleThreadPoll.getInstance().executeSingle(new Runnable() {
            @Override
            public void run() {
                TCPTools.getInstance().startTCPTools();
                UDPTools.getInstance().startUDPTools(NetDefine.LOCAL_IP_ADDR);
            }
        });

        findViewById(R.id.callButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SingleThreadPoll.getInstance().executeSingle(new Runnable() {
                    @Override
                    public void run() {
                        if (0 == IntercomSystem.getInstance().call(NetDefine.REMOT_IP_ADDR)) {
                            Log.e(TAG, "run: 发送呼叫操作成功");
                        }

                    }
                });
            }
        });

        findViewById(R.id.hangupButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SingleThreadPoll.getInstance().executeSingle(new Runnable() {
                    @Override
                    public void run() {
                        if (IntercomSystem.getInstance().hangup(NetDefine.REMOT_IP_ADDR) == 0) {
                            Log.e(TAG, "run: 发送挂断指令成功");
                        }
                        ringingTimeCountDownUnit.stopCountDown();
                    }
                });
            }
        });

        ((SurfaceView) findViewById(R.id.decodeDisplaySurfaceView)).getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                h264VideoPlayer.startVideoDecoder(((SurfaceView) findViewById(R.id.decodeDisplaySurfaceView)));
                UDPTools.getInstance().setOnVideoDataListener(h264VideoPlayer.getDataListener());
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                UDPTools.getInstance().setOnVideoDataListener(null);
                h264VideoPlayer.stopVideoDecoder();
            }
        });
        ((SurfaceView) findViewById(R.id.decodeDisplaySurfaceView2)).getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                h264VideoPlayer2.startVideoDecoder(((SurfaceView) findViewById(R.id.decodeDisplaySurfaceView2)));
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                h264VideoPlayer2.stopVideoDecoder();
            }
        });
        ((SurfaceView) findViewById(R.id.decodeDisplaySurfaceView3)).getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                h264VideoPlayer3.startVideoDecoder(((SurfaceView) findViewById(R.id.decodeDisplaySurfaceView3)));
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                h264VideoPlayer3.stopVideoDecoder();
            }
        });
        ((SurfaceView) findViewById(R.id.decodeDisplaySurfaceView4)).getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                h264VideoPlayer4.startVideoDecoder(((SurfaceView) findViewById(R.id.decodeDisplaySurfaceView4)));
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                h264VideoPlayer4.stopVideoDecoder();
            }
        });
        ((SurfaceView) findViewById(R.id.cameraPreviewSurfaceView)).getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                CameraTools.getInstance().initCamera(((SurfaceView) findViewById(R.id.cameraPreviewSurfaceView)));
                CameraTools.getInstance().startCamera(new Camera.PreviewCallback() {
                    @Override
                    public void onPreviewFrame(byte[] data, Camera camera) {
                        h264VideoCollector.addOriginVideoData(data);
                    }
                });
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                CameraTools.getInstance().stopCamera();
                CameraTools.getInstance().releaseCamera();
            }
        });
        //endregion

        IntercomSystem.getInstance().addIntercomSystemCallBack(new IntercomSystem.IntercomSystemCallBack() {
            @Override
            public void onCall(String remoteIpAddr) {
                Log.e(TAG, "onCall: 远端设备请求监视");
            }

            @Override
            public void onRinging(String remoteIpAddr) {
                Log.e(TAG, "onRinging: 远端设备开始响铃：" + remoteIpAddr + "  当前设备开始响铃");
                startVideo();
                ringingTimeCountDownUnit = new RingingTimeCountDownUnit();
                TimeCountDownTools.getInstance().addTimeCountDownUnit(ringingTimeCountDownUnit);
            }

            @Override
            public void onAccept(String remoteIpAddr) {
                Log.e(TAG, "onAccept: ");
            }

            @Override
            public void onHangUp(String remoteIpAddr) {
                Log.e(TAG, "onHangUp: ");
            }

            @Override
            public void onBusy(String remoteIpAddr) {
                Log.e(TAG, "onBusy: ");
            }

            @Override
            public void onTimeout(String remoteIpAddr) {
                Log.e(TAG, "onTimeout: ");
            }

            @Override
            public void onUnlock(String remoteIpAddr) {
                Log.e(TAG, "onUnlock: ");
            }
        });

    }

    private void startVideo() {
        SingleThreadPoll.getInstance().executeSingle(new Runnable() {
            @Override
            public void run() {
                h264VideoCollector.startVideoEncoder(new OnDataListener() {
                    @Override
                    public void onData(Trans_Package trans_package) {
//                        Log.e(TAG, "onData: 发送到第三方设备");
                        h264VideoPlayer.getDataListener().onData(trans_package);
                        h264VideoPlayer2.getDataListener().onData(trans_package);
                        h264VideoPlayer3.getDataListener().onData(trans_package);
                        h264VideoPlayer4.getDataListener().onData(trans_package);
                    }
                }, NetDefine.REMOT_IP_ADDR, 0x6877);
//                h264VideoCollector.startVideoEncoder(NetDefine.REMOT_IP_ADDR, 0x6877);
            }
        });
    }

    private void stopVideo() {
        SingleThreadPoll.getInstance().executeSingle(new Runnable() {
            @Override
            public void run() {
                h264VideoCollector.stopVideoEncoder();
            }
        });
    }

}
